package com.zzyl.framework.config;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
import com.zzyl.framework.config.properties.HuaweiIotProperties;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.huaweicloud.sdk.core.region.Region;

@Configuration
public class HuaweiIotConfig {

    @Autowired
    private HuaweiIotProperties huaweiIotProperties;

    /**
     * 创建IoTDAClient实例并初始化
     * @return
     */
     @Bean
     public IoTDAClient client(){

         Region region = new Region( huaweiIotProperties.getRegionId(), huaweiIotProperties.getEndpoint());
         // 创建IoTDAClient实例并初始化
         IoTDAClient client = IoTDAClient.newBuilder()
                 .withCredential(getAuth())
                 // 标准版/企业版：需自行创建Region对象，基础版：请使用IoTDARegion的region对象，如"withRegion(IoTDARegion.CN_NORTH_4)"
                 .withRegion(new Region( huaweiIotProperties.getRegionId(), huaweiIotProperties.getEndpoint()))
                 // .withRegion(IoTDARegion.CN_NORTH_4)
                 // 配置是否忽略SSL证书校验， 默认不忽略
                 // .withHttpConfig(new HttpConfig().withIgnoreSSLVerification(true))
                 .build();
         return client;
     }

     //创建认证
     public ICredential getAuth() {
         ICredential auth = new BasicCredentials()
                 .withAk(huaweiIotProperties.getAccessKey())
                 .withSk(huaweiIotProperties.getSecretKey())
                 // 标准版/企业版需要使用衍生算法，基础版请删除配置"withDerivedPredicate"
                 .withDerivedPredicate(BasicCredentials.DEFAULT_DERIVED_PREDICATE)
                 .withProjectId(huaweiIotProperties.getProjectId());
         return auth;
     }


}
